<template>
	<el-container>

		<el-header style="height:auto">
			<div class="left-panel">
				<el-button type="primary" icon="el-icon-plus" @click="table_add" plain>添加</el-button>
			</div>
			<div class="right-panel">
				<div class="right-panel-search">
					<scSearch @upsearch="upsearch" >
							<template #item>
							<el-form-item label="菜单名称：">
								<el-input v-model="search.menuName" placeholder="菜单名称" @keyup.enter="upsearch" clearable></el-input>
							</el-form-item>
							<el-form-item label="状态：">
								<el-select v-model="search.status" @change="upsearch">
										<el-option :value="0" label="正常"></el-option>
										<el-option :value="1" label="停用"></el-option>
								</el-select>
							</el-form-item>
							</template>
					</scSearch>
				</div>
			</div>
		</el-header>
		<el-main class="nopadding">
			<scTable ref="table" :data="tableData" :params="search" :row-key="'menuId'" @selection-change="selectionChange" remoteSort stripe hidePagination v-loading="loading" :expand-row-key="openIds"  >
				<el-table-column prop="menuName" label="菜单名称" :show-overflow-tooltip="true" width="160"></el-table-column>
      <el-table-column prop="icon" label="图标" align="center" width="100">
        <template #default="scope">
          <!-- <svg-icon :icon-class="scope.row.icon" /> -->
					<!-- <el-icon><sc-icon-item-detail  :class="scope.row.icon"/></el-icon> -->
					<el-button type="text" :icon="scope.row.icon" v-if="scope.row.icon && scope.row.icon != '#'"></el-button>
					<span v-else>--</span>
        </template>
      </el-table-column>
      <el-table-column prop="orderNum" label="排序" width="60"></el-table-column>
      <el-table-column prop="perms" label="权限标识" :show-overflow-tooltip="true"></el-table-column>
      <el-table-column prop="component" label="组件路径" :show-overflow-tooltip="true"></el-table-column>
      <el-table-column prop="status" label="状态" width="80">
        <template #default="scope">
					<el-tag :type="!scope.row.status ? 'danger': ''">{{!scope.row.status ? '停用' : '正常'}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime">
        <template #default="scope">
          <span>{{ this.$TOOL.dateFormat(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
			<el-table-column label="操作" fixed="right" align="center" width="170">
					<template #default="scope">
						<el-button-group>
							<el-button text type="primary" size="small" @click="table_edit(scope.row, 'edit')">修改</el-button>
							<el-button text type="primary" size="small" @click="table_add(scope.row, 'add')">新增</el-button>
							<el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
								<template #reference>
									<el-button text type="danger" size="small">删除</el-button>
								</template>
							</el-popconfirm>
						</el-button-group>
					</template>
				</el-table-column>
			</scTable>
		</el-main>
	</el-container>
	<!-- 新增/编辑 -->
	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSaveSuccess" @closed="dialog.save=false"></save-dialog>

</template>

<script>
	import saveDialog from './save'
	export default {
		name: '/systemSetting/menu',
		components: {
			saveDialog,
		},
		data() {
			return {
				dialog: {
					save: false,
					permission: false
				},
				selection: [],
				openIds: [],
				tableData: [],
				enmuArr: {},
				loading:false,
				search: {
					menuName: '',
					status: null
				}
			}
		},
		created() {
			this.getList()
		},
		methods: {
			/** 查询菜单列表 */
			async getList() {
				let search = this.search
				this.loading = true
				var res = await this.$API.auth.getNavMenu.get(search);
				this.loading = false
				let data = this.$TOOL.handleTree(res.data, "menuId");
				this.tableData = data
				let openIds = data.map(item => item.menuId)
				this.openIds = openIds

			},
			//添加
			table_add(row){
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open().setData(row)
				})
			},
			//编辑
			table_edit(row){
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open('edit').setData(row)
				})
			},
			//删除
			async table_del(row){
				var reqData = {menuId: row.menuId}
				var res = await this.$API.auth.editMenu.delete(reqData);
				this.getList()
				if(res.code == 200){
					this.$message.success("删除成功")
				}
			},
			//本地更新数据
			handleSaveSuccess(){
				// this.$refs.table.refresh()
				this.getList()
			},
			//搜索
			upsearch(type){
				let search = this.search
				if(type == 'clear') {
					search.menuName = ''
					search.menuName = null
				}
				this.getList()
			},
		}
	}
</script>

<style>
</style>
